package pe.tnova.generic.structure.project.springmvc.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class PluginUtil {

	public static Shell getShell() {
		return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
	}

	public static String getStringBuilderFromInputStream(InputStream is) {
		if (is == null) {
			return null;
		}
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();
		String line;
		try {
			br = new BufferedReader(new InputStreamReader(is));
			while ((line = br.readLine()) != null) {
				sb.append(line + ConstantesTNOVA.EN);
			}
		} catch (IOException e) {

		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}

	public static String getStringFromInputStream(InputStream is) {
		return getStringBuilderFromInputStream(is);
	}

	public static String getNameFile(String elementName) {
		return elementName.substring(0, elementName.indexOf("."));
	}

	public static String capitalizarPrimerCaracter(String elementName) {
		elementName = Character.toUpperCase(elementName.charAt(0)) + (elementName.length() > 1 ? elementName.substring(1) : "");
		return elementName;
	}

	public static String getCamporDesdeMetodoGET(String elementName) {
		elementName = elementName.substring(3, elementName.length());
		elementName = Character.toLowerCase(elementName.charAt(0)) + (elementName.length() > 1 ? elementName.substring(1) : "");
		return elementName;
	}

	public static String getTipoparamtroInstanceOf(String typeSignature) {
		if (typeSignature.indexOf("Boolean") > 0) {
			return "Boolean";
		} else if (typeSignature.indexOf("String") > 0) {
			return "String";
		} else if (typeSignature.indexOf("Date") > 0) {
			return "Date";
		} else if (typeSignature.indexOf("Integer") > 0) {
			return "Integer";
		} else if (typeSignature.indexOf("int") > 0) {
			return "int";
		} else if (typeSignature.indexOf("I") > 0) {
			return "int";
		} else if (typeSignature.indexOf("boolean") > 0) {
			return "boolean";
		} else if (typeSignature.indexOf("char") > 0) {
			return "char";
		} else if (typeSignature.indexOf("Character") > 0) {
			return "Character";
		} else if (typeSignature.indexOf("Long") > 0) {
			return "Long";
		} else if (typeSignature.indexOf("long") > 0) {
			return "long";
		} else
			return "Object";
	}
	
	public static String getClassJava(String typeSignature) {
		if (typeSignature.indexOf("Boolean") > -1) {
			return "Boolean";
		} else if (typeSignature.indexOf("String") > -1) {
			return "String";
		} else if (typeSignature.indexOf("Date") > -1) {
			return "Date";
		} else if (typeSignature.indexOf("Integer") > -1) {
			return "Integer";
		} else if (typeSignature.indexOf("int") > -1) {
			return "int";
		} else if (typeSignature.indexOf("I") > -1) {
			return "int";
		} else if (typeSignature.indexOf("boolean") > -1) {
			return "boolean";
		} else if (typeSignature.indexOf("char") > -1) {
			return "char";
		} else if (typeSignature.indexOf("Character") > -1) {
			return "Character";
		} else if (typeSignature.indexOf("Long") > -1) {
			return "Long";
		} else if (typeSignature.indexOf("long") > -1) {
			return "long";
		}
		return null;
	}
	

	public static String getString(String tagName, Element element) {
		NodeList list = element.getElementsByTagName(tagName);
		if (list != null && list.getLength() > 0) {
			NodeList subList = list.item(0).getChildNodes();

			if (subList != null && subList.getLength() > 0) {
				return subList.item(0).getNodeValue();
			}
		}

		return null;
	}

	public static Document parseXmlDom(InputStream is) {

		org.w3c.dom.Document document = null;
		try {

			// getting the default implementation of DOM builder
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

			dbf.setValidating(false);
			dbf.setIgnoringComments(false);
			dbf.setIgnoringElementContentWhitespace(true);
			dbf.setNamespaceAware(true);

			DocumentBuilder builder = dbf.newDocumentBuilder();

			// parsing the XML file
			document = builder.parse(is);

		} catch (Exception e) {
			// catching all exceptions
			System.out.println();
			System.out.println(e.toString());
		}
		return document;
	}

	public static InputStream getIO(String str) {
		return new ByteArrayInputStream(str.getBytes());
	}

	public static boolean isNull(List lst) {
		return (lst == null || lst.isEmpty());
	}

	public static boolean isNull(String str) {
		return (str == null || str.length() == 0);
	}

	public static boolean isNull(Object obj) {
		return (obj == null);
	}

	public static boolean existenEspacios(String text) {
		if (text.indexOf(ConstantesTNOVA.SPACE) > 0) {
			return true;
		}
		return false;
	}

	public static boolean isNull(Object[] parameterTypes) {
		if (parameterTypes == null || parameterTypes.length == 0) {
			return true;
		}
		return false;
	}

	public static String getTipoAtributoSingnature(String typeSignature) {
		if(typeSignature.equals("I")){
			return "I";
		}
		return typeSignature.substring((typeSignature.indexOf("Q")+1), typeSignature.length()-1);
	}
}
